package com.htu.courseService.Talk.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.htu.courseService.Talk.service.TalkCommentService;
import com.htu.courseService.Talk.service.TalkInfoService;
import com.htu.courseService.Talk.vo.*;
import com.htu.courseService.common.vo.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/talk")
@RequiredArgsConstructor
@Slf4j
public class TalkInfoController {

    private final TalkInfoService talkInfoService;
    private final TalkCommentService talkCommentService;

    @PostMapping("/upload")
    public R<TalkFileDto> upload(@RequestParam("file") MultipartFile file){
        TalkFileDto talkInfoDto = talkInfoService.uploadFile(file);
        return R.success(talkInfoDto);
    }

    /**
     * 提交帖子
     * @param talkInfoDto
     * @return
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody TalkInfoDto talkInfoDto){
        talkInfoService.add(talkInfoDto);
        return R.success();
    }

    /**
     * 查看帖子列表
     * @param talkQuery
     * @return
     */
    @PostMapping("/page")
    public R<IPage<TalkListDto>> page(@RequestBody TalkQuery talkQuery){
        IPage<TalkListDto> talkListDtoList = talkInfoService.list(talkQuery);
        return R.success(talkListDtoList);
    }

    /**
     * 根据talkid获取帖子详情
     * @param id
     * @return
     */
    @GetMapping("/getTalkDetail")
    public R<TalkInfoDto> getTalkDetail(Integer id){
        TalkInfoDto talkInfoDto = talkInfoService.getTalkDetail(id);
        return R.success(talkInfoDto);
    }

    /**
     * 上传评论
     * @return
     */
    @PostMapping("/addComment")
    public R<?> addComment(@RequestBody TalkCommentDto talkCommentDto){
        talkCommentService.addComment(talkCommentDto);
        return R.success();
    }

    /**
     * 查看评论树形结构
     * @param talkId
     * @return
     */
    @GetMapping("/tree")
    public R<List<Tree<Integer>>> list(Integer talkId){
        List<Tree<Integer>> comment = talkCommentService.getCommentList(talkId);
        return R.success(comment);
    }

    /**
     *查找我的帖子列表
     * @param talkQuery
     * @return
     */
    @PostMapping("/myTalkPage")
    public R<IPage<TalkListDto>> myTalkPage(@RequestBody TalkQuery talkQuery){
        IPage<TalkListDto> talkListDtoList = talkInfoService.getMyTalkList(talkQuery);
        return R.success(talkListDtoList);
    }

    /**
     * 我参与评论的帖子
     * @param talkQuery
     * @return
     */
    @PostMapping("/myCommentPage")
    public R<IPage<TalkListDto>> myCommentPage(@RequestBody TalkQuery talkQuery){
        IPage<TalkListDto> talkListDtoList = talkInfoService.getMyCommentList(talkQuery);
        return R.success(talkListDtoList);
    }
}
